package com.study.hot.jetpack.viewmodel

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.SavedStateViewModelFactory
import androidx.lifecycle.ViewModelProvider
import com.study.hot.R
import com.study.hot.databinding.ActivityViewmodelSavestateBinding

/**
 * <pre>
 *     author : fauks
 *     time   : 2020/03/29
 *     desc   : 使用ViewModel状态保存，无需在Activity中使用 onSaveInstanceState(Bundle)方法
 * </pre>
 */
class ViewModelSaveStateActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val saveModelBinding = DataBindingUtil.setContentView<ActivityViewmodelSavestateBinding>(this, R.layout.activity_viewmodel_savestate)
        // 实例化ViewModel
        val data = ViewModelProvider(this, SavedStateViewModelFactory(application, this))[SaveStateViewModel::class.java]
        saveModelBinding.data = data
        saveModelBinding.lifecycleOwner = this
    }
}